There are two modes included in tmap:
"plot" for static mapping and "view" for
interactive mapping. See introduction.
The "view" mode uses the JavaScript library Leaflet as
backend.
The extension package tmap.mapgl offers two new modes which
are also interactive: "mapbox" and "maplibre"
which use the JavaScript libraries Mapbox GL and Maplibre GL
respectively. An API key is requires to use "mapbox" (free
for personal use), but "maplibre" is (as the name
suggestions) free for any use.
Note that tmap.mapgl is a bridge between the R packages mapgl and tmap. It makes the functionality of mapgl (making the JavaScript libraries available to R) also available via the tmap user interface.
For this example we’ll create a choropleth of well being per country.
We assign the map to map without showing it.
map = tm_shape(World) +
tm_polygons("well_being",
fill.scale = tm_scale_continuous(values = "pu_gn"))
tmap_mode("plot")
#> ℹ tmap mode set to "plot".
map
#> [tip] Consider a suitable map projection, e.g. by adding `+ tm_crs("auto")`.
#> ----------------W (in)--H (in)--asp---
#>
#> | device 8.00 3.50 2.29 |
#>
#> | plot area 7.68 3.30 2.32 |
#>
#> | facets area 6.92 3.30 2.09 |
#>
#> | map area 6.84 3.30 2.07 |
#>
#> --------------------------------
#>
#> This message is displayed once per session.
mapbox
For "mapbox" an API key is required, which is free for
personal use. See instructions.
library(tmap.mapgl)
tmap_mode("mapbox")
map
maplibre
tmap_mode("maplibre")
#> ℹ tmap mode set to "maplibre".
map3d polygons
tmap.mapgl also features a new layer type,
tm_polygons_3d, which is only available for
"mapbox" and "maplibre".
This map layer is the same as tm_polygons, but one
addition: polygons can be extruded in 3d shape. The visual variable to
control this is called height.
tmap_mode("maplibre")
#> ℹ tmap mode set to "maplibre".
NLD_dist$pop_dens = NLD_dist$population / NLD_dist$area
tm_shape(NLD_dist) +
tm_polygons_3d(height = "pop_dens",
fill = "edu_appl_sci",
fill.scale = tm_scale_intervals(style = "kmeans", values = "-pu_gn"),
fill.legend = tm_legend("Univeristy degree")) +
tm_maplibre(pitch = 45)
#> No legends available in mode "maplibre" for map variables
#> "height"